/**
 * Created by zhengjiachao on 2017/4/16.
 */

ADF.model.CompositeModel = function(){
    ADF.model.CompositeModel.superclass.constructor.call(this);
};
ADF.model.CompositeModel.PROPERTY_CHILDREN = "children";
COM.util.defineClass(ADF.model.CompositeModel, ADF.model.BaseModel,{

    initModel: function() {
        ADF.model.CompositeModel.superclass.initModel.call(this);
        var children = new AGEF.PropertyDescriptor(ADF.model.CompositeModel.PROPERTY_CHILDREN, []);
        var text = new AGEF.PropertyDescriptor("text", this._getShowText(), "", false);
        this.addPropertyDescriptor(children);
        this.addPropertyDescriptor(text);
    },


    addChild: function(child){
        var children = this.getPropertyValue(ADF.model.CompositeModel.PROPERTY_CHILDREN), newChildren = children.slice(0, children.length);
        newChildren.push(child);
        this._initChildLayoutData(child);
        this._initChildLayoutDataEditorType(child.getPropertyDescriptor(AGEF.BaseModel.PROPERTY_LAYOUTDATA));
        child.setParent(this);
        this.setPropertyValue(ADF.model.CompositeModel.PROPERTY_CHILDREN, newChildren);
    },

    //根据自己的规则，初始化子控件的布局数据
    _initChildLayoutData : function(child){
        var layoutdata=new COM.draw2d.Rectangle();
        COM.util.ObjUtil.copyProperty(layoutdata,child.getLayoutData());
        child.setLayoutData(layoutdata);
    },

    _initChildLayoutDataEditorType : function(descriptor){
        descriptor.setEditorType("baseProperty.XYLayoutDataEditor");
    },

    removeChild: function(child){
        var children = this.getPropertyValue(ADF.model.CompositeModel.PROPERTY_CHILDREN), newChildren = children.slice(0, children.length);
        child.removeParent();
        COM.util.Array.removeItem(newChildren, child);
        this.setPropertyValue(ADF.model.CompositeModel.PROPERTY_CHILDREN, newChildren);
    },
    getChildren: function(){
        return this.getPropertyValue(ADF.model.CompositeModel.PROPERTY_CHILDREN);
    },

    fillSerializedJson : function(jsonObject){
        ADF.model.CompositeModel.superclass.fillSerializedJson.call(this, jsonObject);
        var children = this.getChildren();
        jsonObject['children'] = [];
        for(var index in children) {
            jsonObject.children.push(children[index].serialize());
        }
        jsonObject["text"] = this.getPropertyDescriptor("text").serialize();
    },

    _deserialize: function(jsonObject){
        ADF.model.CompositeModel.superclass._deserialize.call(this, jsonObject);
        var children = jsonObject["children"];
        for(var index in children) {
            var childJsonObject = COM.util.ObjUtil.parseJSON(children[index]), modelType = childJsonObject["modelType"], modelConstructor = eval("("+modelType+")");
            var childModel = new modelConstructor();
            childModel.deserialize(children[index]);
            this.addChild(childModel);
        }
        var newProp = new AGEF.PropertyDescriptor();
        newProp.deserialize(jsonObject["text"]);
        this.propertyDescriptors.put(newProp.getName(), newProp);
    },

    
    _getShowText : function(){
        return "composite";
    }

});